// src/main/java/com/bookmanager/controller/ProfileController.java
package com.bookmanager.controller;

import com.bookmanager.model.User;
import com.bookmanager.model.UserProfile;
import com.bookmanager.service.ProfileService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/profile")
public class ProfileController extends HttpServlet {
    private ProfileService profileService = new ProfileService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        try {
            // 获取用户详细信息
            UserProfile userProfile = profileService.getUserProfile(user.getUserId());

            if (userProfile != null) {
                request.setAttribute("userProfile", userProfile);
                request.getRequestDispatcher("profile.jsp").forward(request, response);
            } else {
                response.sendRedirect("dashboard?message=profile_not_found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("dashboard?message=system_error");
        }
    }
}